function validate(form, validatelist)
{
    
    this.save.disabled = true;

    for (var i = 0; i < validatelist.length; i++)
    {
        var fieldname = validatelist[i][0];
        var pattern = validatelist[i][1];
        var script = validatelist[i][2];
        var referenced = null;
        if(validatelist[i].length > 3)
        {
            referenced = validatelist[i][3];
            if(referenced != null)
            {
                referenced = document.getElementById(referenced);
                if(referenced == null)
                {
                    alert('Отсутствует зависимый объект '+validatelist[i][3]+'.');
                    return false;
                }
            }
        }

        if((referenced == null) || (referenced.style.display == ''))
        {
            var ele = form.elements[fieldname];
            switch(pattern)
            {                    
                case "policy_serial":
                    if(!checkmask(PolicySerialMask, ele.value)) {eval(script);return false;}
                    break;
                case "policy_number":
                    if(!checkmask(PolicyNumberMask, ele.value)) {eval(script);return false;}
                    break;
                case "date":
                    pattern = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d";
                    var re = new RegExp(pattern);
                    if (!ele.value.match(re)){eval(script);return false;}
                    break;
                case "required":
                    if (ele.value == ""){eval(script);return false;}
                    break;
                case "selectisnotnull":
                    if(ele.options.value == ""){eval(script);return false;}
                    break;
            }
        }
    }
    return true;
}

function checkmask(mask, value)
{
    var masksymbol = "";
    var symbol = "";
    if(mask == "")
    {
        return true;
    }
    if(mask.length != value.length)
    {
        return false;
    }
    for(var i = 0; i < mask.length; i++)
    {
        masksymbol = mask.substr(i, 1);
        symbol = value.substr(i, 1);
        switch(masksymbol)
        {
            case "9":
                if(!IsNumeric(symbol)){return false;}
                break;
            case "_":
                if(!IsChar(symbol)){return false;}
                break;
            default:
                if(masksymbol!=symbol){return false;}
                break;
        }
    }
    return true;
}

function IsChar(sText)
{
    var ValidChars = "0123456789abcdefghjiklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZабвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
    var Char;
    for (i = 0; i < sText.length; i++) 
    { 
        Char = sText.charAt(i); 
        if(ValidChars.indexOf(Char) == -1) 
        {
            return false;
        }
    }
    return true;
}

function IsNumeric(sText)
{
    var ValidChars = "0123456789";
    var Char;
    for (i = 0; i < sText.length; i++) 
    { 
        Char = sText.charAt(i); 
        if(ValidChars.indexOf(Char) == -1) 
        {
            return false;
        }
    }
    return true;
}